// pages/detail/detail.js
const mta = require('../../../mta_sdk/mta_analysis.js');
var ctx = null;
const app = getApp();
var school = ''
var classs = ''
var name = ''
var bgpic=''
var pic=''
var pid=''
Page({

  data: {
    shareImg: '',
    shared: 0,
    ajaxSuccess:false
  },

  onLoad: function (opt) {
    console.log(opt)
    mta.Page.init();
    if (opt.shared == 1) {
      this.setData({
        shared: 1,
        shareImg: opt.shareImg || '',
        ajaxSuccess: true
      });

    }
    if (opt.name) {
      console.log("aaaa")
      this.makeImg(opt.name, opt.pic, opt.bgpic)

    }
      school = opt.school;
      classs = opt.class;
      name = opt.name;
      bgpic = opt.bgpic;
      pic = opt.pic;
      pid = opt.pid;
  },

  makeImg(name, pic, bgpic) {
    var me = this;
    if (me.data.shared == 1) {
      return false;
    }

    wx.showLoading({
      title: '加载中...',
    })

    
    var sysInfo = wx.getSystemInfoSync();
    var ft = sysInfo.windowWidth * .045;
    var query = wx.createSelectorQuery();
    ctx = wx.createCanvasContext("mycanvas");

    
    query.select('#canvas').boundingClientRect();//获取canvas的元素信息
    query.select('#headstyle').boundingClientRect();//获取canvas的元素信息
    query.exec(res => {
      var w = res[0].width;
      var h = res[0].height;
      var head_w = res[1].width;
      var head_h = res[1].height;

      wx.getImageInfo({
            src: pic,
            success: x => {
              
              ctx.drawImage(bgpic, 0, 0, w, h);
              ctx.save();
              ctx.beginPath();
              ctx.arc(w / 2.07, h / 3.8, head_w / 2, 0, Math.PI * 2, false);
              ctx.fillStyle="red";
              ctx.fill();
              ctx.clip();
              
              ctx.drawImage(x.path, (w - head_w) / 2.23, (h - head_w) / 5.8, head_w*1.3, head_w*1.3);
              ctx.restore();
            
              ctx.setFontSize(ft);
              ctx.fillStyle = '#000';
              ctx.fillText(name, w /3.1, h / 1.71);
              ctx.draw(false, function () {
                app.buldingImg("mycanvas", callBack => {
                  me.setData({
                    shareImg: callBack.data.url,
                    ajaxSuccess: true
                  });
                  wx.hideLoading();
                });
              });
            }
          })

    })

  },
  //保存图片
  saveImg() {
    app.savePic("mycanvas");
  },
  onShareAppMessage: function () {
    console.log(classs)
    return {
      title: '祖国花朵制作',
      desc: '祖国花朵制作',
      path: `/pages/${pid}/${pid}_detail/${pid}_detail?shared=1&shareImg=${this.data.shareImg}`,
      imageUrl: this.data.shareImg,
    }
  },
  onUnload() {
    wx.removeStorageSync("coverpic")
  },
  toPlay() {
    wx.navigateTo({
      url: `/pages/${pid}/${pid}_make/${pid}_make?pid=card`,
    })
  },
  toHome() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  }
})